package tc.alert.tckejisoc.rule;

import java.lang.reflect.Method;

/**
 * author: cyq
 * time: 2024/10/23 17:37
 */
public class HandlerMapping implements FieldMapping {
    private final String target;
    private final Method handlerMethod;

    public HandlerMapping(String target, String className, String methodName)
            throws ClassNotFoundException, NoSuchMethodException {
        this.target = target;
        Class<?> clazz = Class.forName(className);
        this.handlerMethod = clazz.getMethod(methodName, String.class);
    }

    @Override
    public String extractValue(String rawLog) {
        try {
            return (String) handlerMethod.invoke(null, rawLog);
        } catch (Exception e) {
            return "";
        }
    }

    @Override
    public String getTarget() {
        return target;
    }
}